using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;

namespace Wrath.RuleForge
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            using (FileBroker.Debug = new FileBroker.DebugFile(Path.GetDirectoryName(Application.ExecutablePath) + "\\debug.log"))
            {
#if !DEBUG
                try
                {
#endif
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);

                    MainForm mainForm = new MainForm();
                    FormBroker.AddForm("main", mainForm);

                    SplashScreen splashScreen = new SplashScreen();

                    splashScreen.Show();
                    splashScreen.Run();

                    if (splashScreen.DialogResult == DialogResult.OK)
                    {
                        Application.Run(mainForm);

                        mainForm.Dispose();
                        Globals.Modules.Dispose();

                        // Clear Module Cache.
                        string[] modulePaths = Directory.GetDirectories(Globals.RuleForgePaths.ModuleCache);
                        foreach (string path in modulePaths)
                        {
                            try
                            {
                                Directory.Delete(path, true);
                            }
                            catch { }
                        }
                    }
                    else
                    {
                        FileBroker.Debug.WriteLine("PANIC! Cannot find Fantasy Grounds data directory!");
                    }
#if !DEBUG
                }
                catch (Exception ex)
                {
                    FileBroker.Debug.WriteLine("== Exception ==");
                    FileBroker.Debug.WriteLine("{0}:\n{1}", ex.Source, ex.Message);

                    if (ex.InnerException != null)
                    {
                        FileBroker.Debug.WriteLine();

                        FileBroker.Debug.WriteLine("== Inner Exception ==");
                        FileBroker.Debug.WriteLine("{0}:\n{1}", ex.InnerException.Source, ex.InnerException.Message);
                    }

                    FileBroker.Debug.WriteLine();

                    FileBroker.Debug.WriteLine("== StackTrace ==");
                    FileBroker.Debug.WriteLine("{0}", ex.StackTrace);

                    FileBroker.Debug.WriteLine();
                    FileBroker.Debug.WriteLine("== GLOBALS ==");

                    FileBroker.Debug.WriteLine("== FantasyGroundsPaths ==");
                    FileBroker.Debug.WriteLine(" Data: {0}", Globals.FantasyGroundsPaths.Data);
                    FileBroker.Debug.WriteLine(" RuleSets: {0}", Globals.FantasyGroundsPaths.RuleSets);
                    FileBroker.Debug.WriteLine(" Modules: {0}", Globals.FantasyGroundsPaths.Modules);

                    FileBroker.Debug.WriteLine();

                    FileBroker.Debug.WriteLine("== RuleForgePaths ==");
                    FileBroker.Debug.WriteLine(" Data: {0}", Globals.RuleForgePaths.Data);
                    FileBroker.Debug.WriteLine(" Cache: {0}", Globals.RuleForgePaths.Cache);
                    FileBroker.Debug.WriteLine(" ModuleCache: {0}", Globals.RuleForgePaths.ModuleCache);
                    FileBroker.Debug.WriteLine(" Temp: {0}", Globals.RuleForgePaths.Temp);
                    FileBroker.Debug.WriteLine(" Templates: {0}", Globals.RuleForgePaths.Templates);
                    FileBroker.Debug.WriteLine(" UserTemplates: {0}", Globals.RuleForgePaths.UserTemplates);

                    FileBroker.Debug.WriteLine();

                    FileBroker.Debug.WriteLine("== RuleSets ==");
                    for (int i = 0; i < Globals.RuleSets.Count; i++)
                    {
                        FileBroker.Debug.WriteLine(" * {0}", Globals.RuleSets.GetRuleSet(i));
                    }

                    FileBroker.Debug.WriteLine();

                    FileBroker.Debug.WriteLine("== Modules ==");
                    for (int i = 0; i < Globals.Modules.Count; i++)
                    {
                        Globals.ModuleInformation mi = Globals.Modules.GetModule(i);
                        FileBroker.Debug.WriteLine(" * {0}", mi.Name);
                    }

                    FileBroker.Debug.WriteLine();

                    MessageBox.Show("An unhandled error has occured running RuleForge:\n" + ex.Message + "\n\nCheck the debug.log file in the application directory for more information.", "Unhandled Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
#endif

                FileBroker.Debug.Dispose();
            }
        }
    }
}